package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.converter.StringToItemTypeConverter;
import com.atguigu.lease.web.admin.custom.converter.StringToReleaseConverter;
import com.atguigu.lease.web.admin.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author:高尚
 * @data:2025/7/7 18:37
 * @description:  springmvc框架配置converter
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private StringToItemTypeConverter stringToItemTypeConverter;
    @Autowired
    private StringToReleaseConverter stringToReleaseConverter;
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;

    @Value("${login.interceptor.includes}")
    private String[] includes;

    @Value("${login.interceptor.excludes}")
    private String[] excludes;
    @Override
    public void addFormatters(FormatterRegistry registry) {

//        registry.addConverter(stringToItemTypeConverter);
//        registry.addConverter(stringToReleaseConverter);
        registry.addConverterFactory(stringToBaseEnumConverterFactory);
    }

    @Override //配置拦截地址与放行地址  优化：拦截和放行地址我们可以写到统一配置文件中application.yml
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(includes)//拦截地址
                .excludePathPatterns(excludes)//放行地址
                .order(10);//优先级
    }
}
